package com.dxyxx.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.widget.TextView;

import com.dxyxx.android.setting.UserPreferences;
import com.dxyxx.framework.ShoppingCarFragment.ShoppingCarAdapter;

/**
 * 针对所有的监听写一个公有类，传入参数
 * 
 * @author chinesejie
 * 
 */
public class UserDataContentObserver extends ContentObserver {
	private final static String PHONE = "phone";
	private final static String PASSWORD = "password";
	private final static String IMAGE = "image";
	private final static String USERNAME = "username";
	private UserPreferences userP;
	private String phone;
	private ShoppingCarAdapter commonAdapter;
	private List<Map<String, String>> shoppingCarData;
	private Context context;
	private TextView money;
	private TextView username;

	public UserDataContentObserver(Handler handler, Context context, TextView username) {
		this(handler);

		this.shoppingCarData = shoppingCarData;
		this.context = context;
		this.money = money;
		this.phone = phone;
		this.username = username;
	}

	public UserDataContentObserver(Handler handler) {
		super(handler);
	}

	@Override
	public boolean deliverSelfNotifications() {
		return super.deliverSelfNotifications();
	}

	@Override
	public void onChange(boolean selfChange) {
		userP = UserPreferences.getInstance(context);
		ContentResolver conre = context.getContentResolver();
		Cursor cursor =conre.query(UserDataChangeProvider.CONTENT_URI, new String[] { "phone", "password", "image", "price", "username" },  "phone=?", new String[] { phone }, ""); // 降序查询
//		Cursor cursor = DataQueryUtils.getUser(context, userP.getCurrentUser().getPhone());
		Map<String, String> map = new HashMap<String, String>();
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			int passwordColumn = cursor.getColumnIndex(PASSWORD);
			int usernameColumn = cursor.getColumnIndex(USERNAME);
			int imageColumn = cursor.getColumnIndex(IMAGE);
			int phoneColumn = cursor.getColumnIndex(PHONE);
			map.put(PASSWORD, cursor.getString(passwordColumn));
			map.put(USERNAME, cursor.getString(usernameColumn));
			map.put(IMAGE, cursor.getString(imageColumn));
			map.put(PHONE, cursor.getString(phoneColumn));
			break;
		}
		username.setText(map.get(USERNAME));
		cursor.close();

	}
}
